<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="../styles/dpl-min.css" />
    <link rel="stylesheet" href="../styles/bui-min.css" />
    <link rel="stylesheet" href="../styles/main.css" />
    <style type="text/css">
        .bui-calendar .bui-calendar-footer input.x-datepicker-time {
            width:40px;
            height:30px;
        }
    </style>
</head>
<body>

<!--列表页-->
<div class="container">
    <form id="searchForm" class="form-panel">
        <ul class="panel-content">
            <li>
                <span>
                    <label>名称：</label><input type="text" class="control-text" name="pointRule.name"/>
                </span>
                <span>
                    <label>规则编码：</label><input type="text" class="control-text" name="pointRule.action_code"/>
                </span>
                <span>
                    <label>用户级别：</label>
                    <select name="pointRule.user_level">
                        <option value="">全部</option>
                        <option value="1">等级1</option>
                        <option value="2">等级2</option>
                        <option value="3">等级3</option>
                    </select>
                </span>
                <span>
                    <label>是否可重复获取：</label>
                    <select name="pointRule.status">
                        <option value="">全部</option>
                        <option value="0">不能</option>
                        <option value="1">能</option>
                    </select>
                </span>
                <span>
                    <label>状态：</label>
                    <select name="pointRule.status">
                        <option value="">全部</option>
                        <option value="0">失效</option>
                        <option value="1">生效</option>
                    </select>
                </span>
                <button type="button" id="btnSearch" class="button button-primary">搜索</button>
            </li>
        </ul>
    </form>
    <div class="search-grid-container">
        <div id="grid"></div>
    </div>
</div>

<!--表单页-->
<div id="ModelFormDialog" style="display: none">
    <form id="ModelForm" class="form-horizontal">
        <div class="control-group">
            <label class="control-label">规则名称：</label>
            <div class="controls">
                <input class="input-normal" name="pointRule.name" required="true"/>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">规则编码：</label>
            <div class="controls">
                <input class="input-normal" name="pointRule.action_code" id="ruleCode" required="true"/>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">生效时间：</label>
            <div class="controls">
                <input class="input-normal calendar calendar-time" name="pointRule.start_time" required="true"/>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">失效时间：</label>
            <div class="controls">
                <input class="input-normal calendar calendar-time" name="pointRule.expire_time" required="true"/>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">赠送积分：</label>
            <div class="controls">
                <input class="input-normal" type="number" name="pointRule.point_num" required="true"/>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">参与用户级别：</label>
            <div class="controls">
                <select name="pointRule.user_level">
                    <option value="1" selected>等级1</option>
                    <option value="2">等级2</option>
                    <option value="3">等级3</option>
                </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">是否可重复赠送：</label>
            <div class="controls">
                <select name="pointRule.is_repeat">
                    <option value="1" selected>可重复</option>
                    <option value="0" >不可重复</option>
                </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">描述：</label>
            <div class="controls">
                <input class="input-normal" name="pointRule.describe" />
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">状态：</label>
            <div class="controls">
                <select name="pointRule.status">
                    <option value="1">生效</option>
                    <option value="0">失效</option>
                </select>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">需要操作次数：</label>
            <div class="controls">
                <input class="input-normal" type="number" name="pointRule.action_count" value="1" required="true"/>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">最多可领取积分：</label>
            <div class="controls">
                <input class="input-normal" type="number" name="pointRule.max_num" value="0" required="true"/>(0代表无限制)
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">重复间隔时间(天)：</label>
            <div class="controls">
                <input class="input-normal" type="number" name="pointRule.repeat_interval" value="1" required="true"/>
            </div>
        </div>
        <div class="control-group">
            <label class="control-label">是否连续的：</label>
            <div class="controls">
                <select name="pointRule.is_continuous">
                    <option value="false">否</option>
                    <option value="true">是</option>
                </select>
            </div>
        </div>
        <div class="row form-actions actions-bar">
            <div class="span13 offset3 ">
                <button type="submit" class="button button-primary">保存</button>
                <button type="reset" class="button">重置</button>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript" src="../scripts/jquery/jquery-1.8.1.min.js"></script>
<script type="text/javascript" src="../scripts/bui.js"></script>
<script type="text/javascript" src="../scripts/common/search.js"></script>
<script type="text/javascript">
    var URI = "../clmp/pointRule",MODULE='pointRule';
    BUI.use(['common/search', 'bui/overlay', 'bui/form'], function (Search, Overlay, Form) {
        var columns = [
            {dataIndex: 'rule_id', visible: false},
            {title: '名称', dataIndex: 'name', width: '10%'},
            {title: '规则编码', dataIndex: 'action_code', width: '10%'},
            {title: '创建时间', dataIndex: 'create_time', width: '10%'},
            {title: '开始时间', dataIndex: 'start_time', width: '10%'},
            {title: '有效时间', dataIndex: 'expire_time', width: '10%'},
            {title: '赠送积分', dataIndex: 'point_num', width: '5%'},
            {title: '用户级别', dataIndex: 'user_level', width: '5%'},
            {title: '是否可重复获取', dataIndex: 'is_repeat', width: '8%',renderer:function(value){
                return value==0?"不能":"能";
            }},
            {title: '描述', dataIndex: 'describe', width: '10%'},
            {title: '状态', dataIndex: 'status', width: '3%',renderer:function(value){
                return value==0?"失效":"生效";
            }},
            {title: '需要操作次数', dataIndex: 'action_count', width: '10%', visible: false},
            {title: '最大可领取积分限制', dataIndex: 'max_num', width: '8%'},
            {title: '重复间隔时间', dataIndex: 'repeat_interval', width: '10%', visible: false},
            {title: '是否连续的', dataIndex: 'is_continuous', width: '10%', visible: false, renderer:function(value){
                return value ? "是" : "否";
            }},
            {title: '操作', dataIndex: '', width: '7%', renderer: function (value, obj) {
                var editStr = '<shiro:hasPermission name="/clmp/pointRule/*:post"><span class="grid-command btn-edit" title="编辑">编辑</span></shiro:hasPermission>';
                var delStr = '<shiro:hasPermission name="/clmp/pointRule/*:delete"><span class="grid-command btn-del" title="删除">删除</span></shiro:hasPermission>';
                return editStr + delStr;
            }}
        ];
        var store = Search.createStore(URI, {totalProperty: 'total'});
        var gridCfg = Search.createGridCfg(columns, {
            tbar: {
                items: [
                    <shiro:hasPermission name="/clmp/pointRule:post">
                    {text: '<i class="icon-plus"></i>新建', btnCls: 'button button-small', handler: function () {
                        modelFormObject.clearFields();
                        modelFormObject.set('action', URI);
                        modelFormObject.set('method', "POST");
                        modelFormDialog.show();
                        $("#ruleCode").attr("readonly", false);
                    }},
                    </shiro:hasPermission>
                    {
                        <shiro:hasPermission name="/clmp/pointRule/*:delete">
                        text: '<i class="icon-remove"></i>删除', btnCls: 'button button-small', handler: delFunction
                        </shiro:hasPermission>
                    }
                ]
            },
            emptyDataTpl : '<div style="padding: 20px" class="centered"><img src="../img/nodata.png"><h2>好像还没有数据</h2></div>',
            plugins: [BUI.Grid.Plugins.CheckSelection]
        });

        var search = new Search({
            store: store,
            gridCfg: gridCfg
        });
        var grid = search.get('grid');
        //删除操作
        function delFunction() {
            var selections = grid.getSelection();
            delItems(selections);
        }

        function delItems(items) {
            var ids = [];
            BUI.each(items, function (item) {
                ids.push(item.rule_id);
            });

            if (ids.length) {
                BUI.Message.Confirm('确认要删除选中的记录么？', function () {
                    $.ajax({
                        url: URI + "/" + ids.join("-"),
                        dataType: 'json',
                        type: 'DELETE',
                        data: {ids: ids},
                        success: function (data) {
                            if (data.status == 1) {
                                BUI.Message.Alert('操作成功');
                                search.load();
                            } else {
                                BUI.Message.Alert('操作失败');
                            }
                        }
                    });
                }, 'question');
            }
        }

        //监听事件，删除一条记录
        grid.on('cellclick', function (ev) {
            var sender = $(ev.domTarget); //点击的Dom
            if (sender.hasClass('btn-del')) {
                delItems([ev.record]);
            } else if (sender.hasClass('btn-edit')) {
                edit(ev.record);
            }
        });

        function edit(record) {
            modelFormObject.clearFields();
            modelFormObject.set('action', URI + "/" + record.rule_id);
            modelFormObject.set('method', "POST");
            $("#ruleCode").attr("readonly","readonly");

            for (var name in record) {
                modelFormObject.setFieldValue(MODULE+'.' + name, record[name]);
            }
            modelFormDialog.show();
        }

        var modelFormDialog = new Overlay.Dialog({
            title: '表单',
            width: 800,
            height: 800,
            mask: false,
            buttons: [],
            contentId: 'ModelFormDialog'
        });

        var modelFormObject = new Form.HForm({
            srcNode: '#ModelForm',
            submitType: 'ajax',
            callback: function (data) {
                if (data.status === 1) {
                    BUI.Message.Alert('操作成功');
                    modelFormDialog.hide();
                    search.load();
                } else {
                    BUI.Message.Alert('操作失败:' + data.msg);
                }
            }
        }).render();
    });
</script>
</body>
</html>